<template>
	<view class="containname">
		<form @submit="editname">
			<view class="iptnamew">
				<input type="text" :value="username"  name="username"
				class="iptname" placeholder="请输入昵称"  maxlength="8"/>
			</view>
			<view class="btnname">
				<button type="primary" form-type="submit">提交</button>
			</view>
		</form>
	</view>
</template>

<script>
	import common from '../../common/common.js'
	export default {
		data() {
			return {
				username:""
			}
		},
		onLoad(e) {
			console.log(e)
			this.username = e.username
		},
		methods: {
			editname(e){
				var me = this
				console.log(e.detail.value.username)
				var newname = e.detail.value.username
				var globaluser = this.getGlobalUser('globaluser')
				console.log(globaluser)
				if(newname == ''){
					uni.showToast({
						title:"昵称不能为空",
						duration:1500,
						icon:"error",
						mask:true
					})
				}else{		
					uni.request({
						url:common.serverUrl+'/user/modifyUserinfo',
						data:{
							"userId":globaluser.id,
							"nickname":newname
						},
						header:{
							"headerUserId":globaluser.id,
							"headerUserToken":globaluser.userUniqueToken
						},
						method:"POST",
						success(res) {
							console.log(res.data)
							if(res.data.status == 200){
								uni.showToast({
									title:"修改昵称成功",
									icon:"success",
									duration:1500,
									mask:true,
									success() {
										uni.setStorageSync("globaluser",res.data.data)
										uni.navigateBack({
											delta:1
										})
									}
								})
								
							}
						}
					})
				}
			}
		}
	}
</script>

<style>
@import url("./editname");
</style>
